package com.software.sqlite.api.admin.v1.controller;

import com.alibaba.fastjson.JSON;
import com.software.sqlite.api.admin.v1.vo.AdminLoginUserVO;
import com.software.sqlite.api.admin.v1.vo.AdminV1Result;
import com.software.sqlite.entity.Page;
import com.software.sqlite.entity.TbOrg;
import com.software.sqlite.service.impl.TbOrgService;
import com.software.sqlite.utils.CommonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@Api(value = "管理端-组织管理", tags = "管理端-组织管理")
@RestController
@Slf4j
@RequestMapping("/admin/v1/org")
@CrossOrigin
public class OrgController extends BaseController {
    @Resource
    private TbOrgService orgService;

    @ApiOperation(value = "分页", notes = "分页")
    @GetMapping("/page")
    public AdminV1Result list(String token,Integer page, Integer limit, String keyWork,String orgName,Long shipId)throws Exception{
        AdminV1Result result=getAdmin(token);
        if(result.getCode()==-1){ return result; }
        AdminLoginUserVO loginUser= (AdminLoginUserVO) result.getData();
        TbOrg org=new TbOrg();
        if(CommonUtils.isNotBlank(keyWork)){
            org= JSON.parseObject(keyWork, TbOrg.class);
        }
        if(CommonUtils.isNotBlank(orgName)){
            org.setOrgName(orgName);
        }
        if(shipId!=null && !shipId.equals(0L))
        {
            org.setShipId(shipId);
        }
        if(!loginUser.getUserType().equals("1"))
        {
            org.setPowerShipIds(loginUser.getPowerShipIds());
        }
        Page pageVO=orgService.selectPage(page,limit,org);
        return  AdminV1Result.success(pageVO.getList(),pageVO.getTotalSize());
    }

    @ApiOperation(value = "列表", notes = "列表")
    @GetMapping("/list")
    public AdminV1Result list(String token,String keyWork)throws Exception{
        AdminV1Result result=getAdmin(token);
        if(result.getCode()==-1){ return result; }
        TbOrg org=new TbOrg();
        return  AdminV1Result.success(orgService.selectList(org));
    }

    @ApiOperation(value = "详情", notes = "详情")
    @GetMapping("/detail")
    public AdminV1Result detail(String token,Long id)throws Exception{
        AdminV1Result result=getAdmin(token);
        if(result.getCode()==-1){ return result; }
        return  AdminV1Result.success(orgService.get(id));
    }

    @ApiOperation(value = "添加", notes = "添加")
    @PostMapping("/add")
    public AdminV1Result add(@RequestBody TbOrg model)throws Exception{
        AdminV1Result result=getAdmin(model.getToken());
        if(result.getCode()==-1){ return result; }
        orgService.edit(model);
        return AdminV1Result.success();
    }

    @ApiOperation(value = "修改", notes = "修改")
    @PostMapping("/update")
    public AdminV1Result update(@RequestBody TbOrg model)throws Exception{
        AdminV1Result result=getAdmin(model.getToken());
        if(result.getCode()==-1){ return result; }
        orgService.edit(model);
        return AdminV1Result.success();
    }

    @ApiOperation(value = "删除", notes = "删除")
    @GetMapping("/del")
    public AdminV1Result del(String token,Long id)throws Exception{
        AdminV1Result result=getAdmin(token);
        if(result.getCode()==-1){ return result; }
        TbOrg model=orgService.get(id);
        if(model!=null){
            model.setUseStatus("deleted");
            orgService.update(model);
        }
        return AdminV1Result.success();
    }

    @ApiOperation(value = "批量添加", notes = "批量添加")
    @PostMapping("/addList")
    public AdminV1Result addList(String token,List<TbOrg> list)throws Exception{
        AdminV1Result result=getAdmin(token);
        if(result.getCode()==-1){ return result; }
        orgService.addList(list);
        return AdminV1Result.success();
    }

    @ApiOperation(value = "批量修改", notes = "批量修改")
    @PostMapping("/updateList")
    public AdminV1Result updateList(String token,List<TbOrg> list)throws Exception{
        AdminV1Result result=getAdmin(token);
        if(result.getCode()==-1){ return result; }
        orgService.updateList(list);
        return AdminV1Result.success();
    }

    @ApiOperation(value = "批量删除", notes = "批量删除")
    @GetMapping("/delList")
    public AdminV1Result delList(String token,String ids)throws Exception{
        AdminV1Result result=getAdmin(token);
        if(result.getCode()==-1){ return result; }
        String[] array=ids.split(",");
        if(array.length>0){
            for(int i=0;i<array.length;i++){
                TbOrg model=orgService.get(CommonUtils.parseLongData(array[i]));
                if(model!=null){
                    model.setUseStatus("deleted");
                    orgService.update(model);
                }
            }
            return AdminV1Result.success();
        }
        return AdminV1Result.fail("删除失败");
    }

    @ApiOperation(value = "批量导入Excel", notes = "批量导入Excel")
    @PostMapping("/importData")
    public AdminV1Result importData(String token)throws Exception{
        AdminV1Result result=getAdmin(token);
        if(result.getCode()==-1){ return result; }
        return AdminV1Result.success();
    }

    @ApiOperation(value = "批量导出Excel", notes = "批量导出Excel")
    @PostMapping("/exportData")
    public AdminV1Result exportData(String token)throws Exception{
        AdminV1Result result=getAdmin(token);
        if(result.getCode()==-1){ return result; }
        return AdminV1Result.success();
    }


}
